package org.dreamfly.core.dao.impl;

import org.dreamfly.core.bean.SzModel;
import org.dreamfly.core.dao.ModelDAO;
import org.dreamfly.core.util.HTMLInputFilter;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import java.util.List;

import static org.hibernate.criterion.Restrictions.*;

/**
 * @author Jhat
 * @date 2010-10-5
 * @time 下午09:39:47
 */
public class ModelDAOImpl extends BaseDAOImpl<SzModel,Long> implements ModelDAO{

    @Override
    public void saveOrUpdate(SzModel model) {
        this.getSession().saveOrUpdate(model);
    }

    @Override
    public List<SzModel> normalSearch(SzModel model, int firstRow, int maxRow) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    
    @Override
	public Long findCount(List<Long> typeIds, String title, String serverName) {
		// TODO Auto-generated method stub
    	if (typeIds == null || typeIds.size() == 0)
			return 0L;
    	StringBuffer bs = new StringBuffer();
    	for (Long l : typeIds) {
			bs.append(",");
			bs.append(l);
		}
    	StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from ");
		sql.append(" ");
		sql.append(SzModel.class.getName());
		sql.append(" ");
		sql.append("where typeId in(");
		sql.append(bs.toString().substring(1));
		sql.append(")");
        if(null != serverName){
            sql.append(" and ");
            sql.append("server = '");
			sql.append(serverName);
            sql.append("'");
        }
		if (title == null || title.isEmpty())
			return 0L;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return 0L;
		sql.append(" and (title like '%" + title + "%'");
        sql.append(" or note like");
            sql.append("'%");
			sql.append(title);
			sql.append("%')");
		List list = this.getSession().createQuery(sql.toString()).list();
		return (null == list||list.isEmpty()) ? 0L : (Long)list.get(0);
	}
    
    
	@Override
	public List<SzModel> findByType(List<Long> typeIds, String title,
                                    int firstRow, int maxRow, String serverName) {
		/**
		 * 判断空值
		 */
		if (typeIds == null || typeIds.size() == 0)
			return null;
		/**
		 * 将空格全部去掉
		 */
		if (title == null || title.length() == 0)
			return null;
		title = title.replace(" ", "");
		if (title.isEmpty())
			return null;
		/**
		 * 开始查询操作
		 */
		Criteria criteria = this.getSession().createCriteria(SzModel.class);
		criteria.add(in("szModelType.typeId", typeIds));
        if(null != serverName){
        criteria.add(eq("server",serverName));
        }
		/**
		 * 不为空的状况下 执行查询操作
		 */
		if (title != null && title.length() > 0)
			criteria.add(or(like("title", "%" + title + "%"),like("note", "%" + title + "%")));
		/**
		 * 按标题来排序
		 */
		criteria.addOrder(Order.asc("title"));
		criteria.setFirstResult(firstRow);
		criteria.setMaxResults(maxRow);
		List list = criteria.list();

		return ((null == list) || list.isEmpty()) ? null : list;
	}

	@Override
    public void saveInfo(SzModel model) {
        HTMLInputFilter inputFilter = new HTMLInputFilter();
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();

		SzModel SzModel = (SzModel) session.load(SzModel.class, model.getId());
		SzModel.setTitle(inputFilter.filter(model.getTitle()));
        SzModel.setServer(inputFilter.filter(model.getServer()));
        SzModel.setNote(inputFilter.filter(model.getNote()));
        SzModel.setMinPictureUrl(model.getMinPictureUrl());
        tx.commit();
		session.flush();
    }

    public void modifyInfo(SzModel model){
        HTMLInputFilter inputFilter = new HTMLInputFilter();
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();

		SzModel SzModel = (SzModel) session.load(SzModel.class, model.getId());
		SzModel.setTitle(inputFilter.filter(model.getTitle()));
        SzModel.setNote(inputFilter.filter(model.getNote()));
        tx.commit();
		session.flush();
    }
}
